//导入fs模块
const fs = require('fs')

//构建一个读取文件的函数
function readFile() {

    //构建的promise对象将异步代码同步化
    return new promise((resolve, reject) => {

        //异步代码读取文件
        fs.readFile(path, (err, data) => {
            if (!err) {
                resolve(data)
            } else {
                reject(err)
            }
        })
    })
}
readFile("./1.txt")
    //带失函数写法
    .then(res => {
        console.log("读取到的结果是" + res);
    })

    .catch(err => {
        console.Log(err);
    })

readFile("./2.txt")
    .then(res => {
        console.log('误取到的结果是' + res);
    })
//完成了写入文件时函数的封装
function writeFile(path, content) {

    new promise((resolve, reject) => {
        fs.writeFile(path, content, err => {
            if (!err) {
                resolve()
            } else {
                reject(err)
            }
        })
    })
}
writeFile('./1.txt', 'hhh')
    .then(res => {
        console.log(res);
    })
    .catch(err => {
        console.log(err);
    })
writeFile('./2.txt', '12345')